#!/usr/bin/perl

$event_filter = "[rs-]";
@ptype_filter = ("encap", "tcp", "ack", "cbr", "exp");

# trace MAC instead of AGT
# (ns doesn't install trace objects at the ports of all Mobile Nodes!)
$ttype_filter = "MAC";

if (@ARGV < 1) {
    printf "usage final_route.pl <trace file>\n";
    exit;
}

#yuyuyu
@send_recv = ();
@time_ = ();
@node_addr = ();
@t_type = ();
@etc_ = ();
@pkt_id = ();
@trp_type = ();
@orig_addr = ();
@dest_addr = ();
@route_seqnum = ();
@temp_ = ();

if ($ARGV[0] =~ /^-[sS]/) {
    $y_size = $ARGV[1];
    shift; shift;
}


$tracename = shift;
if ($tracename =~ /(.+)\.tr/) {
    $outname = $1 . ".png";
} else {
    $outname = $tracename . ".png";
}

open(infile, $tracename) or die "couldn't open $tracename";

open(outfile,">filter_route.tmp") or die "couldn't open filter_route.tmp";
if($outfile eq "") {
	printf {outfile} "%-6s%-14s%-6s%-6s%-4s%-7s%-7s%-12s -> %s\n", "event", "Time", "Node", "Layer", "---", "PktID", "PkType", "Orig_Addr", "Dest_Addr";
}

$seq_num = 0;

#Line search
while ($line = <infile>) {

    next if ($line !~ /^$event_filter/);
    $cont = 0;
    foreach $t (@ptype_filter) {
        if ($line =~ /$t/) {
	    $cont = 1;
	    last;
	}
    }
    next unless ($cont);

    @entry = split(/[\s\(\)\[\]\:]+/, $line);

    #"MAC" check
    next if ($entry[3] ne $ttype_filter);
    #next if ($entry[3] ne $ttype_filter && $entry[3] ne "RTR"); 

    $send_recv[$seq_num] = $entry[0];
    $time_[$seq_num] = $entry[1];
    $node_addr[$seq_num] = $entry[2];
    $t_type[$seq_num] = $entry[3];
    $etc_[$seq_num] = $entry[4];
    $pkt_id[$seq_num] = $entry[5];
    $trp_type[$seq_num] = $entry[6];
    $orig_addr[$seq_num] = $entry[13];
    $dest_addr[$seq_num] = $entry[15];
    $route_seqnum[$seq_num] = $seq_num;

    #sort
    for($i = $seq_num; $i > 0; $i--) {
    	if( $pkt_id[$i - 1] <= $pkt_id[$i] ) {
	    last;
	}
	else {
	     #exchange
	     $temp[0] = $send_recv[$i-1];
             $temp[1] = $time_[$i-1];
	     $temp[2] = $node_addr[$i-1];
	     $temp[3] = $t_type[$i-1];
	     $temp[4] = $etc_[$i-1];
	     $temp[5] = $pkt_id[$i-1];
	     $temp[6] = $trp_type[$i-1];
	     $temp[7] = $orig_addr[$i-1];
	     $temp[8] = $dest_addr[$i-1];

             $send_recv[$i-1] = $send_recv[$i];
             $time_[$i-1] = $time_[$i];
             $node_addr[$i-1] = $node_addr[$i];
             $t_type[$i-1] = $t_type[$i];
             $etc_[$i-1] = $etc_[$i];
             $pkt_id[$i-1] = $pkt_id[$i];
             $trp_type[$i-1] = $trp_type[$i];
             $orig_addr[$i-1] = $orig_addr[$i];
             $dest_addr[$i-1] = $dest_addr[$i];

	     $send_recv[$i] = $temp[0];
             $time_[$i] = $temp[1];
             $node_addr[$i] = $temp[2];
             $t_type[$i] = $temp[3];
             $etc_[$i] = $temp[4];
             $pkt_id[$i] = $temp[5];
             $trp_type[$i] = $temp[6];
             $orig_addr[$i] = $temp[7];
             $dest_addr[$i] = $temp[8];
	}
    } #end for($i = $seq_num; $i < 0; $i--)

    $seq_num++;

} #end while()

#write file < - sort data
foreach $number_count (@route_seqnum) {
    printf {outfile} "%-6s%-14s%-6s%-6s%-4s%-7s%-7s%-12s -> %s\n", $send_recv[$number_count] ,$time_[$number_count], $node_addr[$number_count], $t_type[$number_count], $etc_[$number_count], $pkt_id[$number_count], $trp_type[$number_count], $orig_addr[$number_count], $dest_addr[$number_count];
}
close(infile);
close(outfile);
#sort end

#Output file Start!
@output_filename_store = ();
$out_file_num = 0;
$out_file_check = 0;
@frist_form = ();
@change_form = ();
$form_num = 0;
$form_end_num = 0;
$orig_dest;
@route_file_name = ();
$file_check = 0;
$file_num = 0;
$orig_dest_check = 0;
@output_data = ();
$out_data_num = 0;
@all_orig_dest_store = ();
$all_orig_dest_num = 0;

for ($x = $form_end_num ; $x < $seq_num; $x++)
{

    $orig_dest_check = 0;
    $out_file_check = 0;
    #X == 0
    if ($x == 0) {
        $orig_dest = $orig_addr[$x] . "_" .  $dest_addr[$x];   #$orig_addr + $dest_addr (ex 0_24)
        $route_file_name[$file_num] = $orig_dest;
        $change_form[$file_num] = $node_addr[$x];
        $frist_form[$file_num] = $node_addr[$x];
        $output_data[$out_data_num] = $x;
        
        #open output file
        $output_filename_store[$out_file_num] = "outfile_$orig_dest";
        open($output_filename_store[$out_file_num],">tmp_route_$orig_dest.log") or die "couldn't open tmp_route_$orig_dest.log";
        $all_orig_dest_store[$all_orig_dest_num] = $orig_dest;
        printf "output_filename_store[$out_file_num] = %s\n", $output_filename_store[$out_file_num]; ###
        $all_orig_dest_num++;

	printf {$output_filename_store[$out_file_num]} "%-6s%-14s%-6s%-6s%-7s%-7s%-12s -> %s\n", 
           "event", "Time", "Node", "Layer", "PktID", "PkType", "Orig_Addr", "Dest_Addr";
	
        $out_file_num++;

        #open debug.log
        open(outfile2,">debug.log") or die "couldn't open tmp_route_$orig_dest.log";
	printf {outfile2} "X = 0 - change_form[$file_check] = %s x = %s Orig_Dest = %s\n", $change_form[$file_check], $x, $orig_dest;
        printf {outfile2} "X = 0 - output_data[$out_data_num] = %s x = %s\n", $node_addr[$output_data[$out_data_num]], $x;
        printf {outfile2} "%s  %-14s%-6s%-6s%-4s%-7s%-7s%-12s -> %s\n", 
          $send_recv[$x] ,$time_[$x], $node_addr[$x], $t_type[$x], $etc_[$x], $pkt_id[$x], $trp_type[$x], $orig_addr[$x], $dest_addr[$x];

	$out_data_num++;
	$file_num++;
    }

    #Packet ID equal
    elsif($x != 0 && $pkt_id[$x] == $pkt_id[$x - 1]) {
        $orig_dest = $orig_addr[$x] . "_" .  $dest_addr[$x];   #$orig_addr + $dest_addr (ex 0_24)

        for ($y = 0; $y < $file_num; $y++) {
	    if ($route_file_name[$y] eq $orig_dest) {
	        $file_check = $y;
		last;
	    }
	}
        if ($node_addr[$x] ne $node_addr[$x - 1]){
	    $change_form[$file_check] = $change_form[$file_check] . $node_addr[$x];
	    $output_data[$out_data_num] = $x;

            printf {outfile2} "Packet ID equal - output_data[$out_data_num] = %s x = %s\n", $node_addr[$output_data[$out_data_num]], $x;
            printf {outfile2} "%-6s%-14s%-6s%-6s%-4s%-7s%-7s%-12s -> %s\n", 
              $send_recv[$x] ,$time_[$x], $node_addr[$x], $t_type[$x], $etc_[$x], $pkt_id[$x], $trp_type[$x], $orig_addr[$x], $dest_addr[$x];
            printf {outfile2} "Packet ID equal - change_form[$file_check] = %s x = %s Orig_Dest = %s\n", $change_form[$file_check], $x, $orig_dest;

	    $out_data_num++;
        }
    }

    #Packet ID different 
    else {
       #before Check ------------------------------------------------------------------------------------------------------------
        for ($y = 0; $y < $file_num; $y++) {
	    if ($route_file_name[$y] eq $orig_dest) {
	        $file_check = $y;
		last;
	    }
	}

        #frist_form != change_form
	if($frist_form[$file_check] ne $change_form[$file_check]) {
	    printf {outfile2} "Packet ID different - frist_form[$file_check] = %s change_form[$file_check] = %s x = %s\n", 
              $frist_form[$file_check],$change_form[$file_check], $x;
	    $frist_form[$file_check] = $change_form[$file_check];
            $change_form[$file_check] = "";
            
            #write file (Debug.log) <- output data
            printf {outfile2} "\n";
            for ($z = 0; $z < $out_data_num; $z++) {
	        printf {outfile2} "%-6s%-14s%-6s%-6s%-7s%-7s%-12s -> %s\n", $send_recv[$output_data[$z]] ,$time_[$output_data[$z]], 
		     $node_addr[$output_data[$z]], $t_type[$output_data[$z]], $pkt_id[$output_data[$z]], 
		     $trp_type[$output_data[$z]], $orig_addr[$output_data[$z]], $dest_addr[$output_data[$z]];
                     
		     #$output_data[$z] = "";
	    }
	    #$out_data_num = 0;
            printf {outfile2} "\n";

            #open output file
            for ($a = 0; $a < $out_file_num; $a++) {
	        if ($output_filename_store[$a] eq "outfile_$orig_dest") {
		    $out_file_check = 1;
                    $out_file_check_num = $a;
		    last;
		}
	    }

            #create & write file
	    if ($out_file_check == 0 ) {
	        $output_filename_store[$out_file_num] = "outfile_$orig_dest";
		printf "output_filename_store[$out_file_num] = %s\n", $output_filename_store[$out_file_num]; ###
                open($output_filename_store[$out_file_num],">tmp_route_$orig_dest.log") or die "couldn't open tmp_route_$orig_dest.log";
                $all_orig_dest_store[$all_orig_dest_num] = $orig_dest;
                $all_orig_dest_num++;

                printf {$output_filename_store[$out_file_num]} "%-6s%-14s%-6s%-6s%-7s%-7s%-12s -> %s\n", 
                   "event", "Time", "Node", "Layer", "PktID", "PkType", "Orig_Addr", "Dest_Addr";

		for ($z = 0; $z < $out_data_num; $z++) {
	            printf {$output_filename_store[$out_file_num]} "%-6s%-14s%-6s%-6s%-7s%-7s%-12s -> %s\n", 
		       $send_recv[$output_data[$z]] ,$time_[$output_data[$z]],
	               $node_addr[$output_data[$z]], $t_type[$output_data[$z]], $pkt_id[$output_data[$z]],
                       $trp_type[$output_data[$z]], $orig_addr[$output_data[$z]], $dest_addr[$output_data[$z]];
		       
                       $output_data[$z] = "";
                }

                printf {$output_filename_store[$out_file_num]} "\n";
                $out_data_num = 0;
		$out_file_num++;
            }
            #write file
	    else {
		for ($z = 0; $z < $out_data_num; $z++) {
	            printf {$output_filename_store[$out_file_check_num]} "%-6s%-14s%-6s%-6s%-7s%-7s%-12s -> %s\n", 
		       $send_recv[$output_data[$z]] ,$time_[$output_data[$z]],
	               $node_addr[$output_data[$z]], $t_type[$output_data[$z]], $pkt_id[$output_data[$z]],
                       $trp_type[$output_data[$z]], $orig_addr[$output_data[$z]], $dest_addr[$output_data[$z]];
		       
                       $output_data[$z] = "";
                }
                printf {$output_filename_store[$out_file_check_num]} "\n";
                $out_data_num = 0;
            }
					
	    
        }
        #frist_form == change_form
        else {
            printf {outfile2} "Packet ID different PASS FORM - frist_form[$file_check] = %s change_form[$file_check] = %s x = %s\n", 
              $frist_form[$file_check],$change_form[$file_check], $x;

            #init output_data[]
            for ($z = 0; $z < $out_data_num; $z++) {
                     $output_data[$z] = "";
            }
            $out_data_num = 0;

        }
       #--------------------------------------------------------------------------------------------------------------------------

        #NEW Packet ID
        $orig_dest = $orig_addr[$x] . "_" .  $dest_addr[$x];   #$orig_addr + $dest_addr (ex 0_24)

        $output_data[$out_data_num] = $x;
        printf {outfile2} "NEW Packet ID - output_data[$out_data_num] = %s x = %s\n", $node_addr[$output_data[$out_data_num]], $x;
        $out_data_num++;


        for ($y = 0; $y < $file_num; $y++) {
	    if ($route_file_name[$y] eq $orig_dest) {
	        $file_check = $y;
		$orig_dest_check = 1;
		last;
	    }
	}

	if ($orig_dest_check != 1) {
	    $route_file_name[$file_num] = $orig_dest;
	    $change_form[$file_num] = $node_addr[$x];
            printf {outfile2} "NEW Packet ID_orig_dest_check != 1 - change_form[$file_num] = %s x = %s Orig_Dest = %s\n", $change_form[$file_num], $x, $orig_dest;
	    $frist_form[$file_num] = $node_addr[$x];
	    $file_num++
	}
	else {
	    $change_form[$file_check] = $node_addr[$x];
            printf {outfile2} "NEW Packet ID_orig_dest_check = 1 - change_form[$file_check] = %s x = %s Orig_Dest = %s\n", $change_form[$file_check], $x, $orig_dest;
	}
	    
    } #end if(x == 0)

} #end for($x = $form_end_num ; $x < $seq_num; $x++)

close(outfile2);
foreach $number_count2 (@output_filename_store) {
    printf "close file = %s\n" , $number_count2;
    close($number_count2);
}

#ALL Traffic File Create
open(outfile_all,">tmp_all_traffic.log") or die "couldn't open tmp_all_traffic.log";
for ($x = 0; $x < $all_orig_dest_num; $x++) {
    printf {outfile_all} "---------- orig_dest = %s ------------------------------\n" ,$all_orig_dest_store[$x];
    open(outfile3,"tmp_route_$all_orig_dest_store[$x].log") or die "couldn't open tmp_route_$all_orig_dest_store[$x].log";
    while(<outfile3>){
        printf {outfile_all} $_;
    }
    printf {outfile_all} "\n\n\n";
    close(outfile3);
}
close(outfile_all);


#system "cat filter_route.tmp | sort +5n -6 +1 > sort_route.tmp";
#system "rm -rf filter_route.tmp";

